package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.Course;
import com.xmy.cultivate.entity.StudentGrade;
import com.xmy.cultivate.service.IStudentGradeService;
import com.xmy.cultivate.service.IStudentService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 学生分班表 前端控制器
 * </p>
 *
 * @author heLin
 * @since 2024-05-08
 */
@RestController
@RequestMapping("/api/studentGrade")
@Scope("prototype")
public class StudentGradeController {
    @Autowired
    @Lazy
    IStudentGradeService iStudentGradeService;

    @GetMapping("/gradeByStudentId")
    public String getGradeByStudentId(@RequestParam(value = "yearPart") Integer yearPart,
                                      @RequestParam(value = "quarterNum") Integer quarterNum,
                                      @RequestParam(value = "studentId") Long studentId) {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("student_id", studentId);
        wrapper.eq("lesson_type", 1);
        wrapper.eq("`status`", 1);
        wrapper.eq("`reading_status`", 1);
        wrapper.eq("`subjects_id`", 1);
        wrapper.eq("year_part", yearPart);
        wrapper.eq("quarter_num", quarterNum);
        wrapper.groupBy("grade_id");
        StudentGrade studentGrade = iStudentGradeService.getOne(wrapper);

        if (studentGrade != null) {
            wrapper.clear();

            wrapper.eq("grade_id", studentGrade.getGradeId());
            wrapper.eq("lesson_type", 1);
            wrapper.eq("`status`", 1);
            wrapper.eq("`reading_status`", 1);
            wrapper.eq("`subjects_id`", 1);
            wrapper.eq("year_part", yearPart);
            wrapper.eq("quarter_num", quarterNum);
            wrapper.groupBy("student_id");
            List<StudentGrade> studentGradeList = iStudentGradeService.list(wrapper);

            StringBuffer sb = new StringBuffer();
            for (StudentGrade grade : studentGradeList) {
                sb.append(grade.getStudentId() + ",");
            }

            String result = sb.toString();
            if (result.endsWith(",")) {
                result = result.substring(0, result.length() - 1);
            }
            return result;

        }
        return null;
    }

}
